﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GKQC.Common
{
    public class DataGridViewColumnStyle
    {
        /// <summary>
        /// 绑定列样式
        /// </summary>
        public static void GetColumnStyle(string userCode, string formName, DataGridView dataGridView)
        {
            try
            {
                string path = Application.StartupPath + @"\Style\DataGridView\" + userCode + "\\" + formName + "_" + dataGridView.Name + ".style";

                //如果不存在则保存列样式
                if (!File.Exists(path))
                {
                    return;
                }

                //加载列样式
                string styleString
                    = Settings.GetDataObjectFile<string>(path);
                if (string.IsNullOrWhiteSpace(styleString)) return;
                List<DataGridViewColumnStyleEntity> styleList = JsonHelper.ToObject<List<DataGridViewColumnStyleEntity>>(styleString);

                if (styleList == null) return;
                foreach (DataGridViewColumnStyleEntity row in styleList)
                {
                    try
                    {
                        if (dataGridView.Columns[row.Name] == null) continue;
                        if (dataGridView.Columns[row.Name].Visible == true)
                        {
                            dataGridView.Columns[row.Name].Width = row.Width;
                            dataGridView.Columns[row.Name].DisplayIndex = row.DisplayIndex;
                        }
                    }
                    catch { }
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        /// <summary>
        /// 保存列样式
        /// </summary>
        public static void SaveColumnStyle(string userCode,string formName, DataGridView dataGridView)
        {
            try
            {
                //如果目录不存在则创建
                string path = Application.StartupPath + @"\Style\DataGridView\" + userCode + "\\" + formName + "_" + dataGridView.Name + ".style";
                string dir = path.Substring(0, path.LastIndexOf('\\'));
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }

                
                List<DataGridViewColumnStyleEntity> styleList = new List<DataGridViewColumnStyleEntity>();
                foreach (DataGridViewColumn col in dataGridView.Columns)
                {
                    if (col.Visible == true)
                    {
                        DataGridViewColumnStyleEntity s = new DataGridViewColumnStyleEntity();
                        s.Name = col.Name;
                        s.Width = col.Width;
                        s.DisplayIndex = col.DisplayIndex;
                        styleList.Add(s);
                    }
                }

                Common.Settings.SaveObject(styleList.ToJson(), path);
            }
            catch (Exception ex)
            {
                //throw ex;
            }
        }


    }

    public class DataGridViewColumnStyleEntity
    {
        public string Name { get; set; }
        public int Width { get; set; }
        public int DisplayIndex { get; set; }
    }

}
